﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using LightSwitchApplication.UserCode;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
namespace LightSwitchApplication
{
    public partial class TimeSheetsEmployeesLoadsEditGrid
    {
        partial void btnFilter_Execute()
        {
            if (this.ToDate == null || this.FormDate == null || this.FormDate > this.ToDate)
            {
                this.ShowMessageBox("Vui lòng kiểm tra lại giá trị ngày nhập","Lỗi", MessageBoxOption.Ok);
                return;
            }

            this.DataWorkspace.wcfServiceData.ExcuteStore4("TimeSheetsLoading", this.FormDate.ToShortDateString(), this.ToDate.ToShortDateString(), this.cboEmployees == null ? "" : this.cboEmployees.EmployeeID, Global.SessionID()).Execute();

            this.LoadBySessionID.Load();

            this.LoadBySessionID.Refresh();
            this.LoadBySessionID.OrderBy(o => o.DateWork).OrderBy(o => o.EmployeeID);  
            this.CloseModalWindow("Group1");
        }

        partial void TimeSheetsEmployeesLoadsEditGrid_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            // Write your code here.
            this.SessionID = Guid.Parse(Global.SessionID());

            this.DataWorkspace.wcfServiceData.ExcuteStore4("TimeSheetsLoading", DateTime.Now.ToShortDateString() , DateTime.Now.ToShortDateString(),"", Global.SessionID()).Execute();
        }

        partial void TimeSheetsEmployeesLoadsEditGrid_Closing(ref bool cancel)
        {
            // Write your code here.
            this.DataWorkspace.wcfServiceData.ExcuteSQL("DELETE TimeSheetsEmployeesLoad WHERE SessionID = '" + Global.SessionID() + "'").Execute(); 
        }

        partial void ExcMorning_Execute()
        {
            // Write your code here.
            foreach(var item in this.LoadBySessionID)
            {
                item.Morning = !item.Morning;
            }
        }

        partial void ExcAfternoon_Execute()
        {
            // Write your code here.
            foreach (var item in this.LoadBySessionID)
            {
                item.Afternoon = !item.Afternoon;
            }
        }

        partial void ExcNight_Execute()
        {
            // Write your code here.
            foreach (var item in this.LoadBySessionID)
            {
                item.Night = !item.Night;
            }
        }

        partial void ExcNormal_Execute()
        {
            // Write your code here.
            foreach (var item in this.LoadBySessionID)
            {
                item.Afternoon = !item.Afternoon;
                item.Morning = !item.Morning;
            }
        }

        partial void ExcAll_Execute()
        {
            // Write your code here.
            foreach (var item in this.LoadBySessionID)
            {
                item.Afternoon = !item.Afternoon;
                item.Morning = !item.Morning;
                item.Night = !item.Night;
            }
        }

        partial void OpenFilterDialog_Execute()
        {
            // Write your code here.
            this.OpenModalWindow("Group1"); 
        }

        partial void PrintTimeSheets_Execute()
        {
            // Write your code here.
            LightSwitchApplication.UserCode.ReportHelper s = new UserCode.ReportHelper();
            s.ReportTitle = "Bảng chấm công";
            s.Parameters.Add("FromDate", this.FormDate);
            s.Parameters.Add("ToDate", this.ToDate);
            s.Parameters.Add("EmployeeID", this.cboEmployees == null ? "" : this.cboEmployees.EmployeeID);
            s.ShowReport("TimeSheets");
        }

        partial void TimeSheetsEmployeesLoadsEditGrid_Created()
        {
            // Write your code here.
            this.FindControl("LoadBySessionID").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(TimeSheetsEmployeesLoadsEditGrid_ControlAvailable);
        }

        void TimeSheetsEmployeesLoadsEditGrid_ControlAvailable(object sender, ControlAvailableEventArgs e)
        {
            System.Windows.Controls.DataGrid grid = e.Control as System.Windows.Controls.DataGrid;
            grid.LoadingRow+=new EventHandler<DataGridRowEventArgs>(grid_LoadingRow);
        }

        void grid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            TimeSheetsEmployeesLoad item = e.Row.DataContext as TimeSheetsEmployeesLoad;
            if (item != null)
            {
                if (item.DateWork.DayOfWeek == DayOfWeek.Sunday)
                {
                    e.Row.Background = new SolidColorBrush(Color.FromArgb(50,0,0,255));
                }
                else
                {
                    e.Row.Background = null;
                }
            }
        }

        partial void btnSumary_Execute()
        {
            // Write your code here.
            this.Application.ShowGetSumaryTimeSheetsSearch(this.FormDate, this.LoadBySessionID.SelectedItem != null ? this.LoadBySessionID.SelectedItem.EmployeeID : "", this.ToDate);
        }
    }
}
